const foodAdFacade = require('../../app/facade/foodAd.facade.js');
const foodFacade = require('../../app/facade/food.facade.js');
const merchantFacade = require('../../app/facade/merchant.facade.js');  
const _  =  require('lodash');

module.exports = {
    createFoodAdByMerchant: createFoodAdByMerchant, 
    getFoodAdByMerchant: getFoodAdByMerchant,
    foodAdUpdateStatus: foodAdUpdateStatus,
    getPassedFoodAd: getPassedFoodAd,
    getUnpassedFoodAd: getUnpassedFoodAd
}

function createFoodAdByMerchant(req,res,next){
    let body = req.body;
    console.log(body);

    var foodAd = {};
    
     foodAd.merchant = body.merchant;
     foodAd.food = body.food;
    console.log('foodAd:...');
    console.log(foodAd);
    foodAd.status = '0';
    foodAd.applytime = new Date();
    foodAdFacade.createFoodAdByMerchant(foodAd,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
            res.json(result);
        }     
    });
}

function getFoodAdByMerchant(req,res,next){
    let icnum = req.params.icnum;
    foodAdFacade.getFoodAdByMerchant(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
             res.json(result);
        }          
    });
}

function getPassedFoodAd(req,res,next){
    let icnum = req.params.icnum;
    foodAdFacade.getPassedFoodAd(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
             res.json(result);
        } 

    });

}

function getUnpassedFoodAd(req,res,next){
    let icnum = req.params.icnum;
    foodAdFacade.getUnpassedFoodAd(icnum,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
             res.json(result);
        } 
    });
}

function foodAdUpdateStatus(req,res,next){
    let body = req.body;
    let fname = body.food;
    let icnum = body.merchant;
    let status = body.status;
    foodAdFacade.foodAdUpdateStatus(fname,icnum,status,(error,result)=>{
        if (error) {
            return next(new Error(error));
        } else {
             res.json(result);
        }         
    });
}

